define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Echarts) {
    var Controller = {
        index: function () {
            console.log(Config.echartsData);
            console.log(Config.echartsCategory);
            var myChart = Echarts.init($('#echarts1')[0], 'walden');
            var option = {
                title: {
                    text: '上报统计',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis',
                    formatter: "{b}<br>{a0}：{c0} 条<br> {a1}：{c1} 积分"
                },
                legend: {
                    data: ['上报数', '奖励积分'],
                    selectedMode:'single',
                },
                toolbox: {
                    show: true,
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: {
                    type: 'category',
                    data: Config.echartsCategory
                },
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series:Config.echartsData,
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
            
            $(".datetimerange").data("callback", function (start, end) {
                var date = start.format(this.locale.format) + " - " + end.format(this.locale.format);
                $(this.element).val(date);
                refresh_echart(date);
            });
            Form.api.bindevent($("#form1"));
            var one = {};
            var refresh_echart = function (date) {
                one && clearTimeout(one);
                one = setTimeout(function () {
                    Fast.api.ajax({
                        url: 'keesuishoupai/dashboard/index',
                        data: {date: date},
                        loading: false
                    }, function (data) {
                            option.xAxis.data = data.category;
                            option.series = data.data;
                            myChart.clear();
                            myChart.setOption(option, true);
                    });
                }, 50);
            };
             //点击按钮
             $(document).on("click", ".btn-filter", function () {
                var label = $(this).text();
                var obj = $(this).closest("form").find(".datetimerange").data("daterangepicker");
                var dates = obj.ranges[label];
                obj.startDate = dates[0];
                obj.endDate = dates[1];

                obj.clickApply();
            });
            //刷新
            $(window).resize(function () {
                myChart.resize();
            });
            //点击刷新
            $(document).on("click", "a.btn-refresh", function () {
                if ($(this).data("type")) {
                    refresh_echart();
                } else {
                    var input = $(this).closest("form").find(".datetimerange");
                    var date = $(input).val();
                    refresh_echart(date);
                }
            });
            
            //每隔一分钟定时刷新图表
            setInterval(function () {
                $(".btn-refresh").trigger("click");
            }, 60000);

            //选项卡切入事件
            $(document).on("click", "#resetecharts", function () {
                setTimeout(function () {
                    $(window).trigger("resize");
                }, 50);
            });

           
        }
    };
    return Controller;
});
